package org.demo.config.extendSqlMethods;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @author yexi
 */
public class DeleteByIdClickHouse extends AbstractMethod {

    /**
     * 修改自
     *
     * @see com.baomidou.mybatisplus.core.injector.methods.DeleteById
     */
    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        String sql;
        ExtendSqlMethod sqlMethod;
        if (tableInfo.isWithLogicDelete()) {
            sqlMethod = ExtendSqlMethod.LOGIC_DELETE_BY_ID_CLICKHOUSE;
            sql = String.format(sqlMethod.getSql(),
                    tableInfo.getTableName(),
                    this.sqlLogicSet(tableInfo),
                    tableInfo.getKeyColumn(),
                    tableInfo.getKeyProperty(),
                    tableInfo.getLogicDeleteSql(true, true));
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
            return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource);
        } else {
            sqlMethod = ExtendSqlMethod.DELETE_BY_ID_CLICKHOUSE;
            sql = String.format(sqlMethod.getSql(),
                    tableInfo.getTableName(),
                    tableInfo.getKeyColumn(),
                    tableInfo.getKeyProperty());
            SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, Object.class);
            return this.addDeleteMappedStatement(mapperClass, sqlMethod.getMethod(), sqlSource);
        }
    }

    @Override
    protected String sqlLogicSet(TableInfo table) {
        return table.getLogicDeleteSql(false, false);
    }

}